home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C24 / RTTIwithReferences.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  709 b   |  30 lines

  1. //: C24:RTTIwithReferences.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <cassert>
  7. #include <typeinfo>
  8. using namespace std;
  9.  
  10. class B {
  11. public:
  12.   virtual float f() { return 1.0;}
  13.   virtual ~B() {}
  14. };
  15.  
  16. class D : public B { /* ... */ };
  17.  
  18. int main() {
  19.   B* p = new D;
  20.   B& r = *p;
  21.   assert(typeid(p) == typeid(B*));
  22.   assert(typeid(p) != typeid(D*));
  23.   assert(typeid(r) == typeid(D));
  24.   assert(typeid(*p) == typeid(D));
  25.   assert(typeid(*p) != typeid(B));
  26.   assert(typeid(&r) == typeid(B*));
  27.   assert(typeid(&r) != typeid(D*));
  28.   assert(typeid(r.f()) == typeid(float));
  29. } ///:~
  30.